*************************************************************
* The following code is part of the replication archive of  * 
* Genovese, Kern & Martin (2016) "Policy Alternation", ISQ  * 
*************************************************************

cd "/Users/.../gkm_replication_isq/carbon policy"

*************
* Table A.8 *
*************

* Focal policy (CO2 tax) weighted by geographical distance + Alternative policy (Co2 Allowances) weighted by geographical distance X ideology

clear
pr drop _all
set more off
set matsize 800

***********************
*Likelihood Evaluator  
***********************                                                         

program define splag_ll

args lnf mu rho1  sigma
tempvar A rSL1 rSL2 
gen double `rSL1'=`rho1'*SL1
*gen double `rSL2'=`rho2'*SL2
*gen double `rSL3'=`rho3'*SL3
scalar p1 = `rho1'
*scalar p2 = `rho2'
*scalar p3 = `rho3'
matrix p1W1 = p1*W1
*matrix p2W2 = p2*W2
*matrix p3W3 = p3*W3
matrix IpW = I_n - p1W1   
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`mu', 0, `sigma'))
end

**********************

* eigenval(E)
spatwmat using "/Users/.../gkm_replication_isq/carbon policy/distance_gleditch_ind.dta", name(W1)  standardize

**************************
*Open Data for Regression
**************************
drop _all
use "/Users/.../gkm_replication_isq/carbon policy/EU neighbors_carbon policy.dta", clear

global nobs = 187 
matrix I_n = I($nobs)
global Y co2taxnormal
global X lag_co2taxnormal gdppercapita gdppercapitasq co2ktpercapita co2ktpercapitasq energydependencektofoileq govteffectiveness euintegration  dpi ratio_over_eutob euintegration
global Z allowances_gdp_lag

mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)

matrix SL1 = W1*Y
svmat SL1, n(SL1)
matrix SL2 = W1*Z
svmat SL2, n(SL2)

mkmat SL2, matrix(G)

* Note: SL2 stands for (Wgeographic distance*CO2 Allowances)

************************
*Produce starting values
************************ 

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

ml model lf splag_ll (mu: $Y= lag_co2taxnormal gdppercapita gdppercapitasq co2ktpercapita  co2ktpercapitasq energydependencektofoileq govteffectiveness euintegration allowances_gdp_lag ratio_over_eutob c.dpi##c.SL2 ) (rho1:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
*ml init rho2:_cons=0
*ml init rho3:_cons=0
ml init sigma:_cons=`OLSsigma'
*ml trace on
ml max

